﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dln.Common;
using Dln.Common.WxPayAPI;
using Dln.Common.Helper;
using Dln.Handler;

namespace Dln.Handler
{
    public class WeChatSdk
    {
        /// <summary>
        /// 获取微信用户
        /// </summary>
        /// <returns></returns>
        public static List<Newtonsoft.Json.Linq.JToken> GetUsersList()
        {
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}", TokenBussiness.GetTokenKey());

            string json = RequestHelper.SendGet(url);
            if (!String.IsNullOrEmpty(json))
            {
                try
                {
                   var obj= Newtonsoft.Json.Linq.JObject.Parse(json);
                    var result = obj["data"]["openid"].ToList();
                    return result;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
            return null;
        }

        public static UserInfoConfig GetUserInfo(string openid)
        {
            if (!String.IsNullOrEmpty(openid))
            {
                string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN ", TokenBussiness.GetTokenKey(), openid));
                
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        UserInfoConfig userInfoConfig = JSONHelper.FromJson<UserInfoConfig>(json);
                        return userInfoConfig;
                    }
                    catch (Exception ex)
                    {
                        Log.Info("WxPayApi", "Report request : " + ex.ToString());
                    }
                }
            }
            return null;

        }
    }
}
